package br.indt.framework.ui.common;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import br.indt.framework.log.MicrologWrapper;
import br.indt.framework.ui.ImageLoader;
import br.indt.framework.utils.StringUtils;

/**
 * Button composed by images
 * 
 * @author Nokia Institute of Technology
 *
 */
public class ButtonImage extends Widget {
	
	private static final MicrologWrapper log = MicrologWrapper.getLogger(ButtonImage.class);
	
	private static final int POST_DETAILS_BUTTON_IMAGE_HEIGHT = 72;
	
	private Label label = null;
	private Label label1 = null;
	private Label label2 = null;
	
	private Image leftBorderImage = null;
	private Image middleBorderImage = null;
	private Image rightBorderImage = null;
	
	private Image leftBorderImageClicked = null;
	private Image middleBorderImageClicked = null;
	private Image rightBorderImageClicked = null;
	
	private Image leftBorderImageFeedClicked = null;
	private Image middleBorderImageFeedClicked = null;
	private Image rightBorderImageFeedClicked = null;
	
	private Image opacityImage = null;
	
	private Image placeHolder = null;

	private boolean isOpaco = false;
	private boolean isOpacoPhoto = false;
	private boolean feedCall = false;

	private int buttonWidth = 225;
	
	private ButtonImageActionListener buttonActionListener = null;
	private FocusListener focusListener = null;

	public ButtonImage(String name, String text, String text2) {
		super(name);
		
//		try {
//			placeHolder = UIManager.getInstance().getImageLoadingImage();
//		} catch (Exception e) {
//			Utils.logError("ButtonImage::Constructor - " + e.getMessage());
//		}
		
		label =  new Label(name, text, false, 1, null);
		label.setFont(Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_MEDIUM));
		label.setTextColor(0x333333);
		label.setMargin(0, 0, 50, 0);
		
		label1 = new Label(name+"1", text2, false, 2, null);
		label1.setFont(Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_SMALL));
		label1.setTextColor(0xA4A3A4);
		label1.setMargin(0, 0, 60, 0);

		Icon icon = ImageLoader.createIcon("addPlaceIcon", "icon_location.png", "icon_location.png", 0Xdddad7);
		label2 = new Label(name+"2", StringUtils.EMPTY, false, 1, icon);
		label2.setFont(Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, Font.SIZE_SMALL));
		label2.setTextColor(0);
		label2.setMargin(0, 0, 80, 0);
		
		setLayoutButton();

		calculateHeight();
	}
	
	public void setOpaco(boolean isOpaco) {
		this.isOpaco = isOpaco;
	}
	
	public void setOpacoPhoro(boolean isOpacoPhoro) {
		this.isOpacoPhoto = isOpacoPhoro;
	}

	public void setButtonImageActionListener(ButtonImageActionListener buttonActionListener) {
		this.buttonActionListener = buttonActionListener;
	}
	
	public void setWidth(int width) {
		
		try {
			label.setWidth(width);
			label1.setWidth(width);
			label2.setWidth(width);
			
			this.width = width;
			calculateHeight();
		} catch (Exception e) {
//			//#ifdef LOGS
//			Utils.log("###>>> ERROR IN ButtonImage::setWidth: " + e);
//			//#endif
		}
	}

	
	public int getButtonWidth() {
		return buttonWidth;
	}

	
	public void setButtonWidth(int width) {
		this.buttonWidth = width;
	}
	
	private void calculateHeight() {
		height =  topMargin + bottomMargin + POST_DETAILS_BUTTON_IMAGE_HEIGHT;
	}
	
	
	public void setText(String msg1, String msg2 ){
		label.setText(msg1);
		label1.setText(msg2);
	}
	
	
	public void setTextPlace(String msg){
		label2.setText(msg);
	}
	
	
	public String getText() {
		return label.getText();
	}

	public void paint(Graphics g, int x, int y, int maxWidth, int maxHeight) {
		try {
			x += leftMargin;
			y += topMargin;

			g.setClip(x, y, buttonWidth, POST_DETAILS_BUTTON_IMAGE_HEIGHT);
			
			drawImage(g,x,y);
			drawButton(g, x, y);

			x+=15;
			y+=10;
			
			if (isOpacoPhoto && backgroundImage != null) {
				label2.paint(g, x, y + 35 , maxWidth, maxHeight);
				y += label.getHeight();
			}else{
			
				if(!label.getText().equals("NOTITLE")){
					label.paint(g, x , y , maxWidth, maxHeight);
					y += label.getHeight();
				}
			}
			
			if(!label1.getText().equals("NOTITLE")){
				label1.paint(g, x , y, maxWidth, maxHeight);
			}
			
		} catch (Exception e) {
			//empty
		}
	}
	
	public void click(int x, int y) {
		
		if(buttonActionListener != null) {
			buttonActionListener.buttonClicked(this);
		}
		setPressedDownState(false);
	}
	
	public void longPressed(int x, int y) {
		click(x, y);
	}
	
	public void pressFire() {
		click(0, 0);
	}

	private void setBorderButtonImage(Graphics g, int x, int y, Image left, Image middle, Image right)
	{
		int i = 0;

		g.drawImage(left, x, y, Graphics.TOP | Graphics.LEFT);

		try 
		{
			int sizeButton = x + buttonWidth - right.getWidth();
			for (i = x + left.getWidth(); i <= sizeButton; i++) 
			{
				g.drawImage(middle, i, y, Graphics.TOP | Graphics.LEFT);
			}
			g.drawImage(right, i, y, Graphics.TOP	| Graphics.LEFT);
		}
		catch (Exception e) 
		{
			//#ifdef LOGS
			log.error("[ERROR] ButtonImage::setBorderButtonImage - " + e);
			//#endif
		}
	}

	
	private void setOpacity(Graphics g, int x, int y, Image op)
	{
		int i = 0;
		int k = 0;
		
		int width = x + buttonWidth  - 10;
		int height = y + POST_DETAILS_BUTTON_IMAGE_HEIGHT - 10;
		
		try {
			for (i = x; i <= width; i+=10) 
			{
				for (k = y; k <= height; k+=10){ 
					g.drawImage(op, i, k, Graphics.TOP | Graphics.LEFT);
				}
			}
			
//			if (UIManager.isPortraitMode()) {
//				if (i > 220 && i < 230) {
//					i = 220;
//					for (k = y; k <= height; k += 10) {
//						g.drawImage(op, i, k, Graphics.TOP | Graphics.LEFT);
//					}
//				}
		//	}else{
				if (i > 300 && i < 310) {
					i = 300;
					for (k = y; k <= height; k += 10) {
						g.drawImage(op, i, k, Graphics.TOP | Graphics.LEFT);
					}
				}
			//}
		}
		catch (Exception e) 
		{
			//#ifdef LOGS
			log.error("[ERROR] ButtonImage::setOpacity - " + e);
			//#endif
		}
	}
	
	
	private void setOpacityPhoto(Graphics g, int x, int y, Image op)
	{
		int i = 0;
		int k = 0;
		
		int width = x + buttonWidth  - 10;
		int height = y + POST_DETAILS_BUTTON_IMAGE_HEIGHT - 10;
		
		try {
			for (i = x; i <= width; i+=10) 
			{
				for (k = y + 42; k <= height; k+=10){ 
					g.drawImage(op, i, k, Graphics.TOP | Graphics.LEFT);
				}
			}
		}
		catch (Exception e) 
		{
			//#ifdef LOGS
			log.error("[ERROR] ButtonImage::setOpacityPhoto - " + e);
			//#endif
		}
	}
	
	public ButtonImageActionListener getButtonImageActionListener() {
		return buttonActionListener;
	}
	
	public void addFocusListener(FocusListener listener){
		this.focusListener = listener;
	}
	
	private void drawImage(Graphics g, int x, int y){
		try{
			if (backgroundImage != null) {
				g.drawImage(backgroundImage, x + (buttonWidth / 2),y + (POST_DETAILS_BUTTON_IMAGE_HEIGHT / 2), Graphics.HCENTER | Graphics.VCENTER);
				if (isOpaco) {
					setOpacity(g, x, y, opacityImage);
				}
				if (isOpacoPhoto) {
					setOpacityPhoto(g, x, y, opacityImage);
				}
			}else{
				g.setColor(0xDDDDDD);
				g.fillRoundRect(x, y, buttonWidth, POST_DETAILS_BUTTON_IMAGE_HEIGHT, 0, 0);
				g.drawImage(placeHolder,x + (buttonWidth / 2), y + (POST_DETAILS_BUTTON_IMAGE_HEIGHT / 2), Graphics.HCENTER | Graphics.VCENTER);
			}
		}
		catch (Exception e) 
		{
			//#ifdef LOGS
			log.error("[ERROR] ButtonImage::drawImage - " + e);
			//#endif
		}
	}
	
	private void drawButton(Graphics g, int x, int y){
		try{
			if ((Display.isTouchAndType() && pressedDown) || (!Display.isTouchAndType() && isSelected())) {

				if(isFeedCall()){
					this.setBorderButtonImage(g, x, y,this.leftBorderImageFeedClicked,
						this.middleBorderImageFeedClicked,this.rightBorderImageFeedClicked);
				}else{
					this.setBorderButtonImage(g, x, y,this.leftBorderImageClicked,
							this.middleBorderImageClicked,this.rightBorderImageClicked);
				}
				
				if (this.focusListener != null){
					this.focusListener.focusGained(this);
				}
			}else{
				this.setBorderButtonImage(g, x, y,this.leftBorderImage,
						this.middleBorderImage,this.rightBorderImage);
				
				if (this.focusListener != null){
					this.focusListener.focusLost(this);
				}
			} 
		}catch (Exception e) 
		{
			//#ifdef LOGS
			log.error("[ERROR] ButtonImage::drawButton - " + e);
			//#endif
		}
	}
	
	private void setLayoutButton(){
		try {
			this.leftBorderImage = ImageLoader.load("/buttonImage/big_leftBorderImage.png");
			this.middleBorderImage = ImageLoader.load("/buttonImage/big_middleBorderImage.png");
			this.rightBorderImage = ImageLoader.load("/buttonImage/big_rightBorderImage.png");
			
			this.leftBorderImageClicked = ImageLoader.load("/buttonImage/big_leftBorderImageClicked.png");
			this.middleBorderImageClicked = ImageLoader.load("/buttonImage/big_middleBorderImageClicked.png");
			this.rightBorderImageClicked = ImageLoader.load("/buttonImage/big_rightBorderImageClicked.png");
			
			this.leftBorderImageFeedClicked = ImageLoader.load("/buttonImage/img_border_big_left_selected.png");
			this.middleBorderImageFeedClicked = ImageLoader.load("/buttonImage/img_border_big_repeater_selected.png");
			this.rightBorderImageFeedClicked =ImageLoader.load("/buttonImage/img_border_big_right_selected.png");
			
			this.opacityImage= ImageLoader.load("/bg_10x10_80.png");
			
		} catch (Exception e) {
			//#ifdef LOGS
			System.out.println("###>>> ERROR IN ButtonImage::setLayoutButton - " + e);
			//#endif
		}
	}

	
	public boolean isFeedCall() {
		return feedCall;
	}

	public void setFeedCall(boolean feedCall) {
		this.feedCall = feedCall;
	}
	
	public void setPlaceHolder(Image placeHolder) {
		this.placeHolder = placeHolder;
	}
	
}
